//
//  PlazaViewController.swift
//  NDAi
//
//  Created by 丸子 on 2024/6/3.
//

import UIKit
import CPFWaterfallFlowLayout

class PlazaViewController: BaseViewController {
    let disposeBag = DisposeBag()
    var viewModel = PlazaViewModel()
    
    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 20, right: 10)
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 10
        let width = (SCREEN_WIDTH - 30) / 2
        layout.itemSize = CGSize(width: width, height: width)
        layout.scrollDirection = .vertical
        // 设置每行的项目数量
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(cellWithClass: PlazaCollectionViewCell.self)
        
        collectionView.backgroundColor = UIColor.clear // 设置背景颜色
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.showsVerticalScrollIndicator = false
        collectionView.emptyDataSetSource = self
        collectionView.emptyDataSetDelegate = self
        return collectionView
    }()
    
    lazy var headerView: PlazaHeaderView = {
       let view = PlazaHeaderView()
        view.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 80)
        return view
    }()
   
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
        view.addSubviews([headerView, collectionView])
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(headerView.snp.bottom)
            make.leading.trailing.equalToSuperview()
            make.bottom.equalTo(self.safeBottom)
        }
        
        viewModel.reloadDataSubject.subscribe { _ in
            self.collectionView.reloadData()
        }.disposed(by: disposeBag)
        self.viewModel.getPlazaList()

    }
    
    func initUI(){
        self.view.backgroundColor = UIColor(hexString: "#F3F2F7")
        self.navigationItem.title = "技能广场"
//        let leftBtn = UIButton(type: .custom)
//        leftBtn.setImage(UIImage(named: "icon_home_left"), for: .normal)
//        let leftItem = UIBarButtonItem(customView: leftBtn)
//        self.navigationItem.leftBarButtonItem = leftItem
        
        let rightBtn = UIButton(type: .custom)
        rightBtn.setImage(UIImage(named: "icon_home_sub"), for: .normal)
        rightBtn.backgroundColor = UIColor(hexString: "#464957")
        rightBtn.setTitle("订阅", for: .normal)
        rightBtn.allRound(12)
        rightBtn.titleLabel?.font = UIFont.pingFangSCRegular(ofSize: 15)
        rightBtn.layoutButtonWithEdgeInsetsStyle(.Left, space: 5)
        rightBtn.contentEdgeInsets = UIEdgeInsets(top: 2, left: 12, bottom: 3, right: 12)
        let rightItem = UIBarButtonItem(customView: rightBtn)
        self.navigationItem.rightBarButtonItem = rightItem
        
        rightBtn.setClosureTouchUpInside_k { [weak self] in
            if Appearance.shared.accoutModel != nil {
                let vc = SubscribeViewController()
                self?.navigationController?.pushViewController(vc, animated: true)
            }else {
                GlobalRx.loginSubject.onNext(())
            }
        }
    }
}

extension PlazaViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, columnForSection section: Int) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.viewModel.list.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let model = self.viewModel.list[indexPath.item]
        let cell:PlazaCollectionViewCell = collectionView.dequeueReusableCell(withClass: PlazaCollectionViewCell.self, for: indexPath)
        cell.iconIV.sd_setImage(with: URL(string: model.img_logo), placeholderImage: UIImage(named: "ai_launch_logo"))
        cell.titleLb.text = model.title ?? ""
        cell.descLb.text = model.descs ?? ""
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let model = self.viewModel.list[indexPath.item]
        let vc = PlazaChatController()
        vc.model = model
        self.navigationController?.pushViewController(vc)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let size = CGSize(width: 100, height: 200)
        return size
    }
}

extension PlazaViewController: DZNEmptyDataSetDelegate, DZNEmptyDataSetSource {
    open func image(forEmptyDataSet scrollView: UIScrollView!) -> UIImage! {
        UIImage(named: "icon_nodata")
    }
    
    open func title(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
        let text = "点击刷新"
        let attributes: [NSAttributedString.Key: Any] = [.font: UIFont.pingFangSCRegular(ofSize: 15), .foregroundColor: Theme.color.text_d3]
        return NSAttributedString(string: text, attributes: attributes)
    }
    
    //    func verticalOffset(forEmptyDataSet scrollView: UIScrollView!) -> CGFloat {
    //        return -navHig()
    //    }
    
    func emptyDataSet(_ scrollView: UIScrollView!, didTap view: UIView!) {
        self.viewModel.getPlazaList()
    }
    
}
